/*
/---------------------------------------------------------------------\
|   This file is part of SAPlink Unit Tests                           |
|                                                                     |
|   SAPlink Unit Tests is free software;                              |
|   you can redistribute it and/or modify                             |
|   it under the terms of the GNU General Public License as published |
|   by the Free Software Foundation; either version 2 of the License, |
|   or (at your option) any later version.                            |
|                                                                     |
|   SAPlink Unit Tests is distributed in the hope that                |
|   it will be useful,                                                |
|   but WITHOUT ANY WARRANTY; without even the implied warranty of    |
|   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the     |
|   GNU General Public License for more details.                      |
|                                                                     |
|   You should have received a copy of the GNU General Public License |
|   along with SAPlink Unit Tests; if not, write to the               |
|   Free Software Foundation, Inc.,                                   |
|   51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA          |
\---------------------------------------------------------------------/
 */

/*
 * Created on Jan 24, 2010
 *
 * Id:       $Id$
 * Revision: $Revision$
 * Modified: $Date$
 *
 * XMLvalidator
 *
 * Code When          Who               What
 * ---- ------------- ----------------- -------------------------------------
 * EP01 Jan 24, 2010  edward@evpnet.com Initial version
 */
package com.evpnet.xmlvalidator;

import junit.framework.TestCase;

public class TestValidator extends TestCase {
	/**
	 * 
	 */
	public void testNoFileNoSchema () {
		XMLvalidator validator = new XMLvalidator();
		int rc = validator.run(null, null);

		assertEquals("Return Code", 5, rc);
		assertEquals("Status", "FAIL", validator.getStatus());
		assertEquals("Message Count", 1, validator.getMessageCount());
		assertEquals("Message", "Exception:URI cannot be null", validator.getMessage(0));

		rc = validator.run("", "");

		assertEquals("Return Code", 3, rc);
		assertEquals("Status", "FAIL", validator.getStatus());
		assertEquals("Message Count", 2, validator.getMessageCount());
		assertEquals("Message", "FATAL Line:1 Column:1 Message:Content is not allowed in prolog.", validator.getMessage(0));
		assertEquals("Message", "SAXException:FATAL", validator.getMessage(1));
	}

	/**
	 * 
	 */
	public void testYesFileNoSchema () {
		XMLvalidator validator = new XMLvalidator();
		int rc = validator.run("does-not-exist", "");

		assertEquals("Return Code", 4, rc);
		assertEquals("Status", "FAIL", validator.getStatus());
		assertEquals("Message Count", 1, validator.getMessageCount());
		assertEquals("Message", "IOException", validator.getMessage(0).substring(0, 11));

		rc = validator.run(".\\test\\test1.xml", "");

		assertEquals("Return Code", 3, rc);
		assertEquals("Status", "FAIL", validator.getStatus());
		assertEquals("Message Count", 2, validator.getMessageCount());
		assertEquals("Message", "ERROR Line:3 Column:3 Message:s4s-elt-character: Non-whitespace characters are not allowed in schema elements other than 'xs:appinfo' and 'xs:documentation'. Saw 'Text'.", validator.getMessage(0));
		assertEquals("Message", "SAXException:ERROR", validator.getMessage(1));

		rc = validator.run(".\\test\\test1.xml", null);

		assertEquals("Return Code", 3, rc);
		assertEquals("Status", "FAIL", validator.getStatus());
		assertEquals("Message Count", 2, validator.getMessageCount());
		assertEquals("Message", "ERROR Line:2 Column:2 Message:cvc-elt.1: Cannot find the declaration of element 'test'.", validator.getMessage(0));
		assertEquals("Message", "SAXException:ERROR", validator.getMessage(1));
	}

	/**
	 * 
	 */
	public void testNoFileYesSchema () {
		XMLvalidator validator = new XMLvalidator();
		int rc = validator.run("", "does-not-exist");

		assertEquals("Return Code", 3, rc);
		assertEquals("Status", "FAIL", validator.getStatus());
		assertEquals("Message Count", 2, validator.getMessageCount());
		assertEquals("Message", "FATAL Line:1 Column:1 Message:Content is not allowed in prolog.", validator.getMessage(0));
		assertEquals("Message", "SAXException:FATAL", validator.getMessage(1));

		rc = validator.run("", ".\\test\\test1.xsd");

		assertEquals("Return Code", 3, rc);
		assertEquals("Status", "FAIL", validator.getStatus());
		assertEquals("Message Count", 2, validator.getMessageCount());
		assertEquals("Message", "FATAL Line:1 Column:1 Message:Content is not allowed in prolog.", validator.getMessage(0));
		assertEquals("Message", "SAXException:FATAL", validator.getMessage(1));

		rc = validator.run(null, ".\\test\\test1.xsd");

		assertEquals("Return Code", 5, rc);
		assertEquals("Status", "FAIL", validator.getStatus());
		assertEquals("Message Count", 1, validator.getMessageCount());
		assertEquals("Message", "Exception:URI cannot be null", validator.getMessage(0));
	}

	/**
	 * 
	 */
	public void testYesFileYesSchema () {
		XMLvalidator validator = new XMLvalidator();
		int rc = validator.run(".\\test\\test1.xml", ".\\test\\test1.xsd");

		assertEquals("Return Code", 3, rc);
		assertEquals("Status", "FAIL", validator.getStatus());
		assertEquals("Message Count", 2, validator.getMessageCount());
		assertEquals("Message", "ERROR Line:2 Column:2 Message:schema_reference.4: Failed to read schema document '.\\test\\test1.xsd', because 1) could not find the document; 2) the document could not be read; 3) the root element of the document is not <xsd:schema>.", validator.getMessage(0));
		assertEquals("Message", "SAXException:ERROR", validator.getMessage(1));

		rc = validator.run(".\\test\\test1.xml", ".\\test\\test.xsd");

		assertEquals("Return Code", 0, rc);
		assertEquals("Status", "OK", validator.getStatus());
		assertEquals("Message Count", 0, validator.getMessageCount());

		rc = validator.run(".\\test\\test2.xml", ".\\test\\test.xsd");

		assertEquals("Return Code", 3, rc);
		assertEquals("Status", "FAIL", validator.getStatus());
		assertEquals("Message Count", 2, validator.getMessageCount());
		assertEquals("Message", "ERROR Line:2 Column:2 Message:cvc-minLength-valid: Value '' with length = '0' is not facet-valid with respect to minLength '1' for type '#AnonType_nametest'.", validator.getMessage(0));
		assertEquals("Message", "SAXException:ERROR", validator.getMessage(1));
	}
}
